package com.yuntsg.ruiijn.paperana.utils.esutils;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: gcr
 * @Date: 2023/8/30 10:42
 */
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Service
public class EsCreate {

    private final ElasticsearchClient client;


    public void start() throws Exception {
        // 创建连接 创建索引库 库名 这是创建临时库
        CreateIndexResponse createIndexResponse =
                client.indices().create(c -> c.index(IndexConfig.SELF_TETX_INDEX)//库名表
                        /*这个是Index名字,可以直接字符串*/
                        .settings(indexSettingsBuilder -> indexSettingsBuilder.numberOfReplicas("0").numberOfShards("1"))
                        .mappings(o -> o
                                //设置字段名称和类型 当前主要是分词就够用了
                                .properties("database_type", i -> i.text(p -> p))
                                .properties("sentence", i -> i.text(p -> p))
                                .properties("database_type", i -> i.text(p -> p))
                                .properties("str", i -> i.text(p -> p
                                        .analyzer("ik_max_word")
                                        .searchAnalyzer("ik_max_word")
                                        .fields("keyword", r -> r.keyword(m -> m.normalizer("lowercase")))))
                                .properties("sub_project", i -> i.text(p -> p))
                                .properties("file_id", i -> i.text(p -> p))
                                .properties("table_id", i -> i.text(p -> p))
                        ));
        // 打印结果
        System.out.println("开启自动创建es 了?");
        System.out.println(createIndexResponse.acknowledged());
    }

}
